package com.net.servlet;

import com.net.dao.UserDao;
import com.net.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "UserServlet", urlPatterns = {"/user"})
public class UserServlet extends HttpServlet {
    private UserDao userDao = new UserDao();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理接收数据是中文的情况
        request.setCharacterEncoding("utf-8");
        //处理服务器端向浏览器端发送的中文数据编码
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String type = request.getParameter("type");
        if (type == null) {
            response.getWriter().print("<script>alert('兄弟，你想干么啊，告诉我啊！');window.history.back();</script>");
            return;
        }
        //放置核心处理代码
        //服务器端获取浏览器端提交的数据
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        if (type.equals("login")) {//登录判断
            //验证数据的合法性
            try {
                if (userDao.isValid(userName, userPassword)) {
                    request.getSession().setAttribute("userIsValid", true);
//                    request.getRequestDispatcher("index.jsp").forward(request, response);
                    response.getWriter().print("ok");
                } else {
                    /* request.getRequestDispatcher("login.html").forward(request, response);*/
//                    response.getWriter().print("<script>alert('登录信息错误！');window.history.back();</script>");
                    response.getWriter().print("NoOk");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else if (type.equals("insert")) {//数据插入
            //用户名和密码不能为空
            if(userName==null||userName.replace(" ","").equals("")||userPassword==null){
                response.getWriter().print("<script>alert('用户名或密码不能为空！');window.history.back();</script>");
                return;
            }
            User user = new User(userName, userPassword);
            try {
                boolean insertResult = userDao.insert(user);
                    if (insertResult) {
                    request.getRequestDispatcher("user/insert.jsp").forward(request, response);
                } else {
                    response.getWriter().print("<script>alert('插入失败');window.history.back();</script>");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        else if(type.equals("list")){
            String keyword = request.getParameter("keyword");
            if(keyword==null)keyword="";
            try {
                List<User> userList = userDao.query(keyword);
                request.setAttribute("userList",userList);
                request.getRequestDispatcher("user/list.jsp").forward(request, response);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
